package com.bbx.taxi.client.Adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bbx.api.sdk.model.official.passanger.Return.Official_ORG;
import com.bbx.api.sdk.model.official.passanger.Return.Official_ORGChilds.Childs;
import com.bbx.taxi.official.R;
/** 
 * @Path com.bbx.taxi.client.Adapter.AddOrganizationAdapter.java
 * @Author 严旭文
 * @Date 2016-3-30 下午3:17:29
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class AddOrganizationAdapter  extends BaseAdapter {
	public static String TYPE = "type";// 类型
	public static String TEXT = "text";
	public static String TYPE_JU = "ju";//哪个局
	public static String TYPE_BU = "bu";// 哪个部门
	public static String TYPE_NUM = "num";// 人数
	private List<String> list;
	List<Childs> list_org;
	private String type;
	private Context main;
	public Map<Integer, Boolean> isSelected;

	public AddOrganizationAdapter(Context context,List<String> list) {
		this.type=TYPE_NUM;
		this.list = list;
		main = context;
		init(0);
	}
	public AddOrganizationAdapter(Context context, String type,List<Childs > list_org) {
		this.type=type;
		this.list_org = list_org;
		main = context;
		init(-1);
	}
	// 初始化
	public void init(int id) {
		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		if(type.equals(TYPE_NUM)){
		for (int i = 0; i < list.size(); i++) {
			if (id == i) {
				isSelected.put(i, true);
			} else {
				isSelected.put(i, false);
			}
		  }
		}else{
			for (int i = 0; i < list_org.size(); i++) {
				if (id == i) {
					isSelected.put(i, true);
				} else {
					isSelected.put(i, false);
				}
			  }
		}
	}

	public void notifyDataSetChanged(int id) {
		this.notifyDataSetChanged();
		init(id);
	}

	@Override
	public int getCount() {
		if(type.equals(TYPE_NUM)){
			return list.size();
		}else{
			return list_org.size();
		}
		
	}

	@Override
	public Object getItem(int position) {
		if(type.equals(TYPE_NUM)){
			return list.get(position);
		}else{
			return list_org.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;

	}
	public int getSelectedId(){
		if(type.equals(TYPE_NUM)){
			for (int i = 0; i < list.size(); i++) {
				if(isSelected.get(i)){
					return i;
				}
			}
		}else{
			for (int i = 0; i < list_org.size(); i++) {
				if(isSelected.get(i)){
					return i;
				}
			}
		}

		return -1;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(main);
			convertView = (View) inflater.inflate(R.layout.item_addorganization, null);
			viewholder = new ViewHolder();
			viewholder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_content);
			viewholder.cb_content= (CheckBox) convertView
					.findViewById(R.id.cb_content);
			viewholder.layout_item= (RelativeLayout) convertView
					.findViewById(R.id.layout_item);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
		
		//判断类型
		if (type.equals(TYPE_JU)) {
			viewholder.tv_content.setText(list_org.get(position).getName());
			if(isSelected.get(position)){
				viewholder.layout_item.setBackgroundColor(main.getResources().getColor(R.color.white));
			}else{
				viewholder.layout_item.setBackgroundDrawable(main.getResources().getDrawable(R.drawable.selector_gray_white));
			}
		} else if (type.equals(TYPE_BU)) {
//			viewholder.cb_content.setVisibility(View.VISIBLE);
			viewholder.tv_content.setText(list_org.get(position).getName());
			viewholder.layout_item.setGravity(Gravity.CENTER);
		}else if (type.equals(TYPE_NUM))  {
//			viewholder.cb_content.setVisibility(View.VISIBLE);
			viewholder.tv_content.setText(list.get(position));
		}

	
		viewholder.cb_content.setChecked(isSelected.get(position));
		if(isSelected.get(position)){
			viewholder.tv_content.setTextColor(main.getResources().getColor(R.color.blue));
			if(!type.equals(TYPE_JU)){
			viewholder.tv_content.setBackgroundDrawable(main.getResources().getDrawable(R.drawable.shape_corners_blue_transparent));
			}
		}else{
			viewholder.tv_content.setTextColor(main.getResources().getColor(R.color.text_nor));
			if(!type.equals(TYPE_JU)){
			viewholder.tv_content.setBackgroundDrawable(main.getResources().getDrawable(R.drawable.selector_corners_blue_transparent));
			}
		}
		
		return convertView;

	}

	public class ViewHolder {
		TextView tv_content;
		CheckBox cb_content;
		RelativeLayout layout_item;
	}
}